package com.hontech.bread.app

import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.telephony.SmsMessage
import com.hontech.bread.R
import com.hontech.bread.ui.dialog.showInfo
import com.hontech.bread.utils.log
import com.hontech.bread.utils.runOnUi
import com.hontech.bread.utils.stringById

class SmsReceiver : BroadcastReceiver() {

    @SuppressLint("UnsafeProtectedBroadcastReceiver")
    override fun onReceive(context: Context?, intent: Intent?) {
        log("sms receiver")

        val bundle = intent?.extras ?: return

        try {
            val pdus = bundle["pdus"] as Array<*>
            val messages = pdus.map {
                SmsMessage.createFromPdu(it as ByteArray)
            }

            for (message in messages) {
                val sender = message.originatingAddress
                val content = message.messageBody
                runOnUi {
                    showInfo(stringById(R.string.t1733460848_28), "$sender ==> $content")
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}